#!/usr/bin/perl

# ------------------------------------------------------------------------------
# --- Creates many PBS scripts for submitting jobs to NYU HPC
# --- Original by Andrea
# --- Re-written by C. Bergey and A. Watkins
# ------------------------------------------------------------------------------

use strict;
use warnings;

# http://search.cpan.org/~kwmak/PBS-Client/lib/PBS/Client.pm
use lib '/scratch/cmb433/pdb_interact/dependencies/PBS-Client-0.09/usr/lib/perl5/site_perl/5.8.8/';
use PBS::Client;

print STDERR "Creating many PBS scripts for submitting jobs to NYU HPC...\n";

# rosetta.gcc64 was built on HPC with command "make gcc64"

# Get PDB file names from the output of find_interface_chains.pl

open (CHAINS, "<interface_chains_pdb.txt");

# Create array to hold all Rosetta commands
my @rosetta_cmds;

foreach my $pdb (<CHAINS>){
	
	chomp $pdb;
	
	# Line from interface chains file is something like:
	# pdb_partner_files/1A0H_A_B.pdb
	
	my $pdb_code = $pdb;
	$pdb_code =~ s/pdb_partner_files\/(.{4})_._.\.pdb/$1/;
	
	print STDERR "PDB is " . $pdb . "\n";
	
	# To get the name of the new PBS script, just change the folder and extension
	
	my $pbs = $pdb;
	$pbs =~ s/pdb_partner_files/pbs_scripts/;
	$pbs =~ s/\.pdb/\.pbs/;
	
	#print "\tPBS is " . $pbs . "\n";
	
	unless (-e $pdb) {
		print STDERR "ERROR: PDB partner file doesn't exist! ($pdb)\n";
		# We should die upon finding a missing file, but for now, keep going
		# This bug has to be fixed eventually
		
		#exit;
	}
	
	# Write PBS script, just in case we want to re-run it later
	
	my $job_name = $pbs;
	$job_name =~ s/pbs_scripts\///;
	$job_name =~ s/\.pbs//;
	
	my $rosetta_cmd = "./rosetta3.4/rosetta_source/bin/rosetta_scripts.linuxgccrelease ";
	$rosetta_cmd .= " -s " . $pdb;
	$rosetta_cmd .= " -use_input_sc -nstruct 20 -jd2:ntrials 2 ";
	$rosetta_cmd .= "-database ./rosetta3.4/rosetta_database/ ";
	$rosetta_cmd .= "-ex1 -ex2 ";
	$rosetta_cmd .= "-parser:protocol rosetta_files/alascan.xml ";
	$rosetta_cmd .= "-parser:view ";
	$rosetta_cmd .= "> rosetta_output/" . $job_name . "_ala_out.txt\n";
	
	push (@rosetta_cmds, $rosetta_cmd);
	
}

close(CHAINS);

# Now write batch PBS scripts

print "Now writing batch PBS scripts...\n";

my $num_cmds = scalar @rosetta_cmds;
my $cmds_per_pbs = 500;

for (my $p = 0; $p < $num_cmds; $p += $cmds_per_pbs) {

	my $bulk_pbs = "pbs_scripts/bulk_ros_" . $p . "_" . ($p + $cmds_per_pbs) . ".pbs";
	
	open (PBS, ">$bulk_pbs")
		or die "ERROR: Could not open PBS script for writing ($bulk_pbs)\n";
    
	print PBS "#PBS -l nodes=1:ppn=1,walltime=20:00:00", "\n";
	print PBS "#PBS -N ",$bulk_pbs, "\n"; 
	print PBS "#PBS -M cmb433\@nyu.edu", "\n";
	print PBS "#PBS -m abe", "\n";
	print PBS "#PBS -o ", $bulk_pbs, ".output", "\n";
	print PBS "#PBS -e ", $bulk_pbs, ".error", "\n";
	print PBS "\n";
	print PBS "cd /scratch/cmb433/pdb_interact/", "\n";
	print PBS "\n";
	
	# Print multiple PBS commands
	for (my $c = $p; $c < $p + $cmds_per_pbs; $c++) {
	
		if ($c >= $num_cmds) {
			last;
		}
	
		print PBS "module load rosetta/openmpi/intel/3.4\n";
	
		print PBS "echo \"CMD: [" . $rosetta_cmds[$c] . "]\"\n";
		print PBS $rosetta_cmds[$c] . "\n";
	}
	
	print PBS "\n";
	print PBS "exit 0;", "\n";
	
	close PBS;

}

exit 0;



